package com.bookStore.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.bookStore.domain.User;

public class AdminDao {
	@SuppressWarnings("finally")
	public User checkLogin(String email, String password){
		User admin = null;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet  rs = null;
		try {
			conn=DBConn.getConnection();
			String sql = "select * from admin where email = ? and password = MD5(?) limit 1";
			ps = conn.prepareStatement(sql);
			ps.setString(1, email);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if(rs.next()){
				admin = new User(rs.getInt("id"), rs.getString("email"), rs.getString("password"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(rs != null){
					rs.close();
				}
				if(ps != null){
					ps.close();
				}
				if(conn != null){
					conn.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			return admin;
		}
	}
}
